import { areaList } from '@vant/area-data';
import { updateUser } from '../../service/user';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    isShowPopup: false,
    name: '',
    mobile: '',
    gender: '',
    address: '',
    detailAddress: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const user = wx.getStorageSync('user')
    this.setData({
      name: user.name,
      mobile: user.mobile,
      gender: String(user.gender),
      address: user.address,
      detailAddress: user.detailAddress
    })
  },
  onClose() {
    this.setData({
      isShowPopup: false
    })
  },
  onShowPopup() {
    this.setData({
      isShowPopup: true
    })
  },
  onConfirm(e: any) {
    const addressArr = e.detail.values
    let address = ''
    addressArr.forEach((item: any) => {
      address += `${item.name} `
    })
    this.setData({
      address,
      isShowPopup: false
    })
  },
  async onUpdateUser() {
    const res = await updateUser({
      name: this.data.name,
      mobile: this.data.mobile,
      gender: Number(this.data.gender),
      address: this.data.address,
      detailAddress: this.data.detailAddress
    })
    wx.setStorage({
      key: 'user',
      data: res.data.user
    })
    wx.showToast({
      title: '修改成功'
    })
  }
})